﻿using System;
using System.Collections.Generic;
using System.Linq;

using RaceList.activity.util;

namespace RaceList.activity.help
{
    /// <summary>
    ///     这里主要是封装了一些数据库中对Activity_Relay,Activity_SubRelay的操作
    /// </summary>
    public partial class RelayQuery
    {
        /// <summary>
        ///     默认为null返回未开始
        /// </summary>
        /// <param name="cycleId"></param>
        /// <returns></returns>
        public static ActivityStatus GetCycleStatus(int cycleId)
        {
            Activity_Cycle cycle = GetCycle(cycleId);
            if (cycle != null)
            {
                if (DateTime.Now >= cycle.startDate && DateTime.Now <= cycle.endDate)
                    return ActivityStatus.Jinxingzhong;
                if (DateTime.Now <= cycle.startDate)
                    return ActivityStatus.WeiKaishi;
                if (DateTime.Now >= cycle.endDate)
                    return ActivityStatus.Jiesu;
            }
            return ActivityStatus.WeiKaishi;
        }


        /// <summary>
        ///     查询当前状态处于进行中的活动（根据时间来判断）
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<Activity_Cycle> GetActive_CycleList()
        {
            IEnumerable<Activity_Cycle> list = GetCycleList();
            list = list.Where(s => s.startDate <= DateTime.Now);
            list = list.Where(s => s.endDate >= DateTime.Now);
            return list;
        }

        #region

        /// <summary>
        ///     查询所有激活的活动数据
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<Activity_Cycle> GetCycleList()
        {
            ipowdb_suibian_webDataContext db = ConnHelper.ipowdb_suibian_web();
            IQueryable<Activity_Cycle> list = db.Activity_Cycle.Where(s => s.status);
            list = list.Where(s => s.isDel == false);
            return list;
        }

        /// <summary>
        ///     得到某个激活的活动数据
        /// </summary>
        /// <param name="cycleId"></param>
        /// <returns></returns>
        public static Activity_Cycle GetCycle(int cycleId)
        {
            ipowdb_suibian_webDataContext db = ConnHelper.ipowdb_suibian_web();
            IEnumerable<Activity_Cycle> list = GetCycleList().Where(s => s.Id == cycleId && s.status);
            Activity_Cycle cycle = list.FirstOrDefault();

            return cycle;
        }

        #endregion
    }
}